package cn.monkey.family.server.domain.uc.auth.wechat;

import cn.monkey.family.data.crud.repository.CrudRepository;
import cn.monkey.family.data.uc.auth.wechat.WechatUser;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.ListQueryByExampleExecutor;

import java.util.Optional;

@NoRepositoryBean
public interface WechatUserRepository extends CrudRepository<WechatUser, String>, ListQueryByExampleExecutor<WechatUser> {
    default Optional<WechatUser> findOneByUidAndOpenidAndAppId(String id, String openid, String appId) {
        WechatUser wechatUser = new WechatUser();
        wechatUser.setUid(id);
        wechatUser.setOpenid(openid);
        wechatUser.setAppId(appId);
        Example<WechatUser> example = Example.of(wechatUser,
                ExampleMatcher.matching().withMatcher("uid", ExampleMatcher.GenericPropertyMatchers.exact())
                        .withMatcher("openId", ExampleMatcher.GenericPropertyMatchers.exact())
                        .withMatcher("appId", ExampleMatcher.GenericPropertyMatchers.exact()));
        return this.findOne(example);
    }
}
